package com.gitee.ywj1352.webFlux;

import org.springframework.context.PayloadApplicationEvent;
import org.springframework.context.event.SimpleApplicationEventMulticaster;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author wenjie.yang on 2019/7/16.
 */
public class Callback {


    public static void main(String[] args) {
        bio();
        nio();
    }

    //不加线程
    public static void bio(){
        SimpleApplicationEventMulticaster multicaster = new SimpleApplicationEventMulticaster();
        multicaster.addApplicationListener(event -> {
            System.out.println(Thread.currentThread().getName() +" :" + event);
        });
        multicaster.multicastEvent(new PayloadApplicationEvent<String>("hello","world"));
    }

    //加线程
    public static void nio(){
        SimpleApplicationEventMulticaster multicaster = new SimpleApplicationEventMulticaster();
        multicaster.addApplicationListener(event -> {
            System.out.println(Thread.currentThread().getName() +" :" + event);
        });
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        multicaster.setTaskExecutor(executorService);
        multicaster.multicastEvent(new PayloadApplicationEvent<String>("hello","world"));
        //关闭线程池
        executorService.shutdown();
    }

}
